package drrr.orm;

import drrr.common.SpringContext;

import javax.persistence.*;
import javax.persistence.Entity;
import java.sql.Timestamp;

/**
 * Created by lirance on 15/5/26.
 */
@Entity
@Table(name = "roomChatList", schema = "", catalog = "drrr")
@IdClass(RoomChatListEntityPK.class)
public class RoomChatListEntity extends drrr.orm.Entity{
    private int roomid;
    private int userid;
    private Timestamp time;
    private String content;

    @Id
    @Column(name = "roomid", nullable = false, insertable = true, updatable = true)
    public int getRoomid() {
        return roomid;
    }

    public void setRoomid(int roomid) {
        this.roomid = roomid;
    }

    @Id
    @Column(name = "userid", nullable = false, insertable = true, updatable = true)
    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    @Id
    @Column(name = "time", nullable = false, insertable = true, updatable = true)
    public Timestamp getTime() {
        /*if (time == null) {
            time = new Timestamp(System.currentTimeMillis());
        }*/
        return time;
    }

    public void setTime(Timestamp time) {
        this.time = time;
    }

    @Basic
    @Column(name = "content", nullable = false, insertable = true, updatable = true, length = 65535)
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 获取用户实体
     * 请忽略错误
     *
     * @return
     */
    public UserEntity getMemberEntity() {
        return ((AllService) SpringContext.getBean("allService")).getUserService().getUserEntity(getUserid());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RoomChatListEntity that = (RoomChatListEntity) o;

        if (roomid != that.roomid) return false;
        if (userid != that.userid) return false;
        if (time != null ? !time.equals(that.time) : that.time != null) return false;
        if (content != null ? !content.equals(that.content) : that.content != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = roomid;
        result = 31 * result + userid;
        result = 31 * result + (time != null ? time.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        return result;
    }
}
